#include "serialport.h"
#include <QTextCodec>
#include <QDebug>
#include <iostream>
serialport::serialport(QObject *parent) : QObject(parent)
{
    serialPort = new QSerialPort;
    /* 串口初始化
       端口：ubuntu：USB0/ARM：ttymxc2（u3）
       波特率：115200
       数据位：8位
       检验位：无
       停止位：1位
       硬件流控制：无
    */
    if(QSysInfo::buildCpuArchitecture() == "arm"){
        serialPort->setPortName("/dev/ttymxc2");
    }else{
        serialPort->setPortName("/dev/ttyUSB0");
    }
    serialPort->open(QIODevice::ReadWrite);
    serialPort->setBaudRate(115200);
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);

    connect(serialPort, SIGNAL(readyRead()),
            this, SLOT(serialPortReadyRead()));


    this->Count = 0;
}


serialport::~serialport()
{

}

void serialport::StringToHex(QString str, QByteArray &senddata)
{
    int hexdata,lowhexdata;
        int hexdatalen = 0;
        int len = str.length();
        senddata.resize(len/2);
        char lstr,hstr;


        for(int i=0; i<len; )
        {
            //char lstr,
            hstr=str[i].toLatin1();
            if(hstr == ' ')
            {
                i++;
                continue;
            }
            i++;
            if(i >= len)
                break;
            lstr = str[i].toLatin1();
            hexdata = ConvertHexChar(hstr);
            lowhexdata = ConvertHexChar(lstr);
            if((hexdata == 16) || (lowhexdata == 16))
                break;
            else
                hexdata = hexdata*16+lowhexdata;
            i++;
            senddata[hexdatalen] = (char)hexdata;
            hexdatalen++;
        }
        senddata.resize(hexdatalen);
}

char serialport::ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))
           return ch-0x30;
       else if((ch >= 'A') && (ch <= 'F'))
           return ch-'A'+10;
       else if((ch >= 'a') && (ch <= 'f'))
           return ch-'a'+10;
       else return ch-ch;//不在0-f范围内的会发送成0
}

void serialport::serialPortReadyRead()
{
    /* 读取数据到字节数组中 */
    QByteArray buff = serialPort->readAll();
    qDebug()<< QString(buff) << endl;

    /*
     车牌识别就位flag
    */
    if(QString(buff) == "carlpr_ready"){
        emit this->openCamera();
        emit this->saveImage(this->CarType,Count);

        Count++;
    }
    /*
     下位机测量体温完毕 开始口罩检测
    */
    if(QString(buff) == "itm_ok"){
        emit this->openCamera();
        emit this->saveImage(this->FaceType,Count);
    }

    /* 体温 */
    if(QString(buff).startsWith("Tmp:")){
        emit this->sqltmp(buff);
    }

    /* 体温异常 */
    if(QString(buff) == "itm_error"){
      //
    }

}

void serialport::sendData(QString senddata1)
{
    QTextCodec *gbk = QTextCodec::codecForName("GBK");

    /*
      因为STM32 帧尾检测0x0d和0xoa 所以要在每个数据流后面加入0d和0a来检测关键帧
    */
    QString sendbuff =senddata1 + "\r\n";
    QByteArray senddata;
    senddata.append(gbk->fromUnicode(sendbuff));
    qDebug()<< senddata <<endl;
    serialPort->write(senddata);
}
